/**
*   类说明
*  
*   @Description   	把数据流转换成String返回 
*   @creator         tangkun
*   @create-time     2011-7-15
*   @revision        $Id
*/

package com.scrawler.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class IOUtil {
	
	/**
	 * 
	 * @param in	IO的数据流
	 * @return		IO转换成的String
	 */
	public static String stream2String(InputStream in)
	{
		return stream2String(in,"UTF-8");
	}
	
	public static String stream2String(InputStream in,String encoding)
	{
		String line = null;
		StringBuffer sb = new StringBuffer();
		BufferedReader buffer = null;
		try {
			 buffer  = new BufferedReader(new InputStreamReader(in,encoding));
			// 读取流数据并拼接到StringBuffer中
			while((line = buffer.readLine()) != null)
			{
				sb.append(line+"\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(buffer != null)
			{
				try {
					buffer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(in != null)
				{
					try {
						in.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return sb.toString(); 
	}
	
	
	
	/**
	 * 一行一行读文件,每行必须以http开头.
	 * @param path
	 * @return	把每行读进到list中
	 */
	
	public static List<String> readFileLineByLine(String path)
	{
		ArrayList<String> array = new ArrayList<String>(500);
		File file = new File(path);
		if(!file.exists())
			try {
				throw new FileNotFoundException();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		try {
			FileInputStream fileStream = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fileStream));
			String line = null;
			while((line = br.readLine()) != null)
			{	
				if(line.startsWith("http://"))
					array.add(line);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return array;
	}
	
}
